package com.yzcrm.erp.kms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;
import com.yzcrm.common.annotation.NotNullValidate;
import com.yzcrm.common.annotation.Permissions;
import com.yzcrm.common.config.paragetter.BasePageRequest;
import com.yzcrm.erp.admin.service.AdminFieldService;
import com.yzcrm.erp.crm.common.CrmEnum;
import com.yzcrm.erp.kms.service.KmsFileService;
import com.yzcrm.swagger.annotation.ActionApi;
import com.yzcrm.swagger.annotation.Api;
import com.yzcrm.swagger.annotation.Param;
import com.yzcrm.swagger.annotation.Params;
import com.yzcrm.utils.R;

/**
 * @author jiang.xue
 * 知识文件管理
 */
@Api(remark = "知识库：知识文件管理")
public class KmsFileController extends Controller{

    @Inject
    private AdminFieldService adminFieldService;

    @Inject
    private KmsFileService kmsFileService;

    /**
     * 查看列表页
     */
    @Permissions({"kms:file:index"})
    @ActionApi(summary = "知识文件列表数据", remark="知识文件列表数据")
    @Params({
            @Param(name="sceneId", remark="场景Id", dataType="int", defaultValue="", required=false),
            @Param(name="type", remark="权限标识 100：文件（file）", dataType="int", defaultValue="", required=true)
    })
    public void queryPageList(BasePageRequest basePageRequest) {

        JSONObject jsonObject = basePageRequest.getJsonObject().fluentPut("type", CrmEnum.KMS_FILE.getType());
        basePageRequest.setJsonObject(jsonObject);
        renderJson(kmsFileService.filterConditionAndGetPageList(basePageRequest));
    }

    /**
     * 添加或者修改知识文件
     */
    @ActionApi(summary = "添加或者修改知识文件", remark="添加或者修改知识文件")
    @Permissions({"kms:file:save", "kms:file:update"})
    public void saveOrUpdate(){

        JSONObject jsonObject = JSON.parseObject(getRawData());
        renderJson(kmsFileService.addOrUpdate(jsonObject));
    }

    /**
     * 根据id删除知识文件
     */
    @ActionApi(summary = "根据id删除知识文件", remark="根据id删除知识文件")
    @Params({
            @Param(name="fileIds", remark="知识文件ID 多个ID,逗号分割", dataType="String", defaultValue="", required=true)
    })
    @Permissions("kms:file:delete")
    @NotNullValidate(value = "fileIds", message = "知识文件id不能为空")
    public void deleteByIds(@Para("fileIds") String fileIds) {
        renderJson(kmsFileService.deleteByIds(fileIds));
    }

    /**
     * 根据知识文件id查询
     */
    @ActionApi(summary = "根据id查询知识文件", remark="根据id查询知识文件")
    @Params({
            @Param(name="fileId", remark="知识文件ID", dataType="String", defaultValue="", required=true)
    })
    @Permissions("kms:file:read")
    @NotNullValidate(value = "fileId", message = "知识文件id不能为空")
    public void queryById(@Para("fileId") Integer fileId) {
        renderJson(R.ok().put("data", kmsFileService.queryById(fileId)));
    }


}
